﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

namespace ConsoleTestXmlJsonTransfer
{
    public class XmlJsonConvertor
    {
        public static string XMLConvertToJsonStr(XmlDocument xmlElement)
        {
            string jsonText = JsonConvert.SerializeObject(xmlElement, Newtonsoft.Json.Formatting.Indented);
            return jsonText;
        }

        public static string XMLConvertToJsonStr(string xmlStr)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xmlStr);
            string jsonText = JsonConvert.SerializeObject(xmlDocument, Newtonsoft.Json.Formatting.Indented);
            return jsonText;
        }


        public static JObject XMLConvertToJObject(XmlDocument xmlElement)
        {
            string jsonText = JsonConvert.SerializeObject(xmlElement, Newtonsoft.Json.Formatting.Indented);
            JObject jObject = JObject.Parse(jsonText);
            return jObject;
        }

        public static JObject XMLConvertToJObject(string xmlStr)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xmlStr);
            string jsonText = JsonConvert.SerializeObject(xmlDocument, Newtonsoft.Json.Formatting.Indented);
            JObject jObject = JObject.Parse(jsonText);
            return jObject;
        }


        public static string JsonConvertToXMLStr(JObject jObject)
        {
            string jsonStr = jObject.ToString();
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument = JsonConvert.DeserializeXmlNode(jsonStr);
            StringWriter sw = new StringWriter();
            using (XmlTextWriter writer = new XmlTextWriter(sw))
            {
                writer.Indentation = 2;  // the Indentation
                writer.Formatting = System.Xml.Formatting.Indented;
                xmlDocument.WriteContentTo(writer);
                writer.Close();
            }
            return sw.ToString();
        }

        public static string JsonConvertToXMLStr(string jsonStr)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument = JsonConvert.DeserializeXmlNode(jsonStr);
            StringWriter sw = new StringWriter();
            using (XmlTextWriter writer = new XmlTextWriter(sw))
            {
                writer.Indentation = 2;  // the Indentation
                writer.Formatting = System.Xml.Formatting.Indented;
                xmlDocument.WriteContentTo(writer);
                writer.Close();
            }
            return sw.ToString();
        }


        public static XmlDocument JsonConvertToXMLElement(JObject jObject)
        {
            string jsonStr = jObject.ToString();
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument = JsonConvert.DeserializeXmlNode(jsonStr);
            return xmlDocument;
        }

        public static XmlDocument JsonConvertToXMLElement(string jsonStr)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument = JsonConvert.DeserializeXmlNode(jsonStr);
            return xmlDocument;
        }
    }
}
